//
//  XMLElement.m
//  XMLBuilder
//
//  Created by shchen on 16/4/5.
//  Copyright © 2016年 shchen. All rights reserved.
//

#import "XMLElement.h"

//用数组实现栈
static NSMutableArray* stack;
//记录深度来控制缩进
static int depth;
@implementation XMLElement
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.childs=[[NSMutableArray alloc]init];
        self.elementAttritude=[[NSMutableDictionary alloc]init];
        stack=[NSMutableArray array];
        depth=0;
    }
    return self;
}

+(XMLElement *)elementWithString:(NSString *)elementName{
    XMLElement* element=[[XMLElement alloc]init];
    [element setElementName:elementName];
    return element;
}

+(XMLElement *)elementWithString:(NSString *)elementName content:(NSString *)content{
    XMLElement* element=[[XMLElement alloc]init];
    [element setElementName:elementName];
    [element setContent:content];
    return element;
}

-(BOOL)addChildElement:(XMLElement *)element{
    //不能是自己
    if(element==self){
        return NO;
    }
    //检查元素是否在数组中,是则返回false
    for(XMLElement* child in _childs){
        if (child==element) {
            return NO;
        }
    }
    [_childs addObject:element];
    //NSLog(@"child=%@",_childs);
    element.parent=self;
    return YES;
}

-(BOOL)deleteChildElement:(XMLElement *)element{
    //检查元素是否在数组中,是则删除,并返回true
    for(XMLElement* child in _childs){
        if (child==element) {
            [_childs removeObject:element];
            element.parent=nil;
            return YES;
        }
    }
    return NO;
}
-(BOOL)deleteChildElementByElementName:(NSString *)elementName{
    //检查元素是否在数组中,是则删除,并返回true
    for(XMLElement* child in _childs){
        if (child.elementName==elementName) {
            [_childs removeObject:child];
            child.parent=nil;
            return YES;
        }
    }
    return NO;
}
-(BOOL)addElementAttritudeForElementAttritudeKey:(NSString *)key forValue:(NSString *)value{
    if ([_elementAttritude valueForKey:key]!=nil) {
        return NO;
    }
    [_elementAttritude setValue:value forKey:key];
    return YES;
}

-(BOOL)deleteElementAttritudeKey:(NSString *)key{
    if ([_elementAttritude valueForKey:key]==nil) {
        return NO;
    }
    [_elementAttritude removeObjectForKey:key];
    return YES;

}
#pragma mark -要求4
//输出所有子节点(包括子孙节点)以及内容
-(NSString *)XMLPrint{
    //递归
    NSMutableString *XML=[[NSMutableString alloc] initWithString:@""];
    [XML appendString:[self XMLPrintHeader]];
    if([self.childs count]==0){
        [XML appendString:[self XMLPrintContent]];
        [XML appendString:[self XMLPrintFooter]];
        return XML;
    }else{
        depth++;
        for (XMLElement* element in self.childs) {
            [XML appendString:@"\n"];
            for (int i=0; i<depth; i++) {
                [XML appendString:@"  "];
            }
            [XML appendString:[element XMLPrint]];
        }
        [XML appendString:@"\n"];
        for (int i=0; i<depth-1; i++) {
            [XML appendString:@"  "];
        }
        [XML appendString:[self XMLPrintFooter]];
        depth--;
    }
    return XML;
}





-(NSString *)XMLPrintHeader{
    NSMutableString* XML=[NSMutableString stringWithFormat:@"<%@",_elementName];
    for(NSString *key in [_elementAttritude allKeys]){
        [XML appendString:[NSString stringWithFormat:@" %@=\"%@\"",key,[_elementAttritude valueForKey:key]]];
    }
    [XML appendString:@">"];
    return XML;
}

-(NSString *)XMLPrintContent{
    return [NSString stringWithFormat:@"%@",_content];
}


-(NSString *)XMLPrintFooter{
    return [NSString stringWithFormat:@"</%@>",_elementName];
}


@end
